package edu.ubbcs.rs.backend.service.filter.manager;

import edu.ubbcs.rs.backend.model.ReservationEvent;
import edu.ubbcs.rs.backend.model.Role;
import edu.ubbcs.rs.backend.model.room.Room;
import edu.ubbcs.rs.backend.service.RoomService;
import edu.ubbcs.rs.backend.service.filter.DateFilter;
import edu.ubbcs.rs.backend.service.filter.EventFilter;
import edu.ubbcs.rs.backend.service.filter.Filter;
import edu.ubbcs.rs.backend.service.filter.HourFilter;
import edu.ubbcs.rs.backend.service.filter.ReserverNameFilter;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;

/**
 * @author Merli Andras-Bertalan
 * @version 1.0
 * @created 09-jan.-2015 15:37:44
 */
@Stateless(name = "FilterService")
public class FilterServiceImpl implements FilterService {

    @Inject
    private RoomService roomService;

    private final List<Filter> filters;

    private final Filter dateFilter;
    private final Filter hourFilter;
    private final Filter reserverNameFilter;
    private final Filter eventFilter;

    public FilterServiceImpl() {
        dateFilter = new DateFilter();
        hourFilter = new HourFilter();
        reserverNameFilter = new ReserverNameFilter();
        eventFilter = new EventFilter();
        filters = new LinkedList<>();
        filters.add(dateFilter);
        filters.add(hourFilter);
        filters.add(reserverNameFilter);
        filters.add(eventFilter);
    }

    @Override
    public List<Room> filter(Role role) {
        List<Room> rooms = roomService.getRoomsByDate((Date) dateFilter.getFilterValue());
        System.out.println("!!!! Rooms size: " + rooms.size());
        FilterIterator filterIterator = new FilterByRoleIterator(filters, role);
        Filter filter = filterIterator.next();
        while (filter != null) {
            filter.filter(rooms);
            System.out.println("!!!! Filter: " + rooms.size());
            filter = filterIterator.next();
        }
        return rooms;
    }

    @Override
    public List<Room> filter() {
        List<Room> rooms = roomService.getRoomsByDate((Date) dateFilter.getFilterValue());
        Iterator<Filter> filterIterator = filters.iterator();
        while (filterIterator.hasNext()) {
            filterIterator.next().filter(rooms);
        }
        return rooms;
    }

    @Override
    public void dateChanged(Date value) {
        dateFilter.setFilterValue(value);
    }

    @Override
    public void hourChanged(Integer value) {
        System.out.println("!!! in hourchanged");
        hourFilter.setFilterValue(value);
    }

    @Override
    public void reserverNameChanged(String value) {
        reserverNameFilter.setFilterValue(value);
    }

    @Override
    public void eventChanged(ReservationEvent reservationEvent) {
        eventFilter.setFilterValue(reservationEvent);
    }
}
